/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ve.com.cantv.worktables.ejb.session;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TemporalType;
import ve.com.cantv.worktables.ejb.entity.Activity;

/**
 *
 * @author Ghust
 */
@Stateless
public class ActivityFacade implements ActivityFacadeRemote {
    @PersistenceContext
    private EntityManager em;

    public Integer create(Activity activity) {
        em.persist(activity);
        Vector obj = (Vector)em.createNativeQuery("SELECT max(idactivity) FROM  activity act").getSingleResult();
        return (Integer)obj.get(0);
    }

    public void edit(Activity activity) {
        em.merge(activity);
    }

    public void remove(Activity activity) {
        em.remove(em.merge(activity));
    }

    public Activity find(Object id) {
        return em.find(Activity.class, id);
    }

    public List<Activity> findAll() {
        return em.createQuery("select object(o) from Activity as o").getResultList();
    }

    public List<Activity> findByConsult(Date startDate, Date endDate, Integer idCategory) {
       List list = em.createQuery("select object(act) from Activity as act where (act.startDate BETWEEN :startDate AND :endDate) AND act.idcategory.idCategoryActivity = :idCategory")
       .setParameter("startDate", startDate)
       .setParameter("endDate", endDate)
       .setParameter("idCategory", idCategory)
       .getResultList();
       System.out.print("TAMAÑO "+list.size());
       ArrayList<Activity> retval = new ArrayList<Activity>();
       for(int i=0;i<list.size();i++)
           retval.add((Activity)list.get(i));
       return retval;
    }

    public List findByConsult(Integer idcategory) {
        List list = em.createQuery("select object(act) from Activity as act where act.idcategory.idCategoryActivity = :idCategory")
       .setParameter("idCategory", idcategory)
       .getResultList();
       ArrayList<Activity> retval = new ArrayList<Activity>();
       for(int i=0;i<list.size();i++)
           retval.add((Activity)list.get(i));
       return retval;
    }

}
